package com.example.administrator.androiddate;

import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.widget.TextView;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends BaseAcivity {
    private String path = "etc/TPV/video";
    private int minute = 1;//这是分钟
    private int second = 0;//这是分钟后面的秒数。这里是以30分钟为例的，所以，minute是30，second是0
    private TextView timeView;
    private TextView timeViewState;
    private Timer timer;
    private TimerTask timerTask;
    private Map<Integer, Integer> mapSRC;
    int flag=0;
    SoundPlayUtils utils;
    //这是接收回来处理的消息
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            if (minute == 0) {
                if (second == 0) {

                    flag++;
                    if(flag==3)
                    {
                        //utils.stop(mapSRC.get(3));
                      //  utils.play(soolId[1],true);
                        if (timer != null) {
                            timer.cancel();
                            timer = null;
                        }
                        if (timerTask != null) {
                            timerTask = null;
                        }
                        finish();
                    }
                    else {
                        if(flag==1)
                        {
                            SoundPoolUtils.getInstence(getApplicationContext()).startSound2(SoundPoolUtils.POP_UP);
//                          utils.stop(mapSRC.get(1));
//                            mapSRC.put(2, utils.loadRes(R.raw.avchat_peer_reject));
//                            utils.play(mapSRC.get(2),true);
                            timeViewState.setText("2");
                        }
                        if(flag==2)
                        {
                            SoundPoolUtils.getInstence(getApplicationContext()).startSound2(SoundPoolUtils.CLICK_RESPONSE);
//                            utils.stop(mapSRC.get(2));
//
//                            mapSRC.put(3, utils.loadRes(R.raw.avchat_peer_reject));
//                            utils.play(mapSRC.get(3),true);
                            timeViewState.setText("3");
                        }
                        //timeView.setText("Time out !");

                        minute=1;

                    }

                } else {
//                    if(flag==0)
//                    {
//                        soolId[1]=utils.loadRes(R.raw.avchat_peer_reject);
//                    }
//                    if(flag==1)
//                    {
//                        soolId[2]=utils.loadRes(R.raw.avchat_no_response);
//                    }
                    second--;
                    if (second >= 10) {
                        timeView.setText("0" + minute + ":" + second);
                    } else {
                        timeView.setText("0" + minute + ":0" + second);
                    }
                }
            } else {
                if (second == 0) {
                    second = 59;
                    minute--;
                    if (minute >= 10) {
                        timeView.setText(minute + ":" + second);
                    } else {
                        timeView.setText("0" + minute + ":" + second);
                    }
                } else {
                    second--;
                    if (second >= 10) {
                        if (minute >= 10) {
                            timeView.setText(minute + ":" + second);
                        } else {
                            timeView.setText("0" + minute + ":" + second);
                        }
                    } else {
                        if (minute >= 10) {
                            timeView.setText(minute + ":0" + second);
                        } else {
                            timeView.setText("0" + minute + ":0" + second);
                        }
                    }
                }
            }
        }

    };

    @Override
    public void timeToDo(boolean flag) {
        super.timeToDo(flag);
        Log.e("=====","时间变化");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss");
        String date=sdf.format(new java.util.Date());
        Log.d("----","time"+date);
        // 流媒体操作类
        timeView = (TextView) findViewById(R.id.tv);
        timeViewState = (TextView) findViewById(R.id.tv_equipmen_state);
        timeView.setText(minute + ":" + second);

        timerTask = new TimerTask() {

            @Override
            public void run() {
                Message msg = new Message();
                msg.what = 0;
                handler.sendMessage(msg);
            }
        };

        timer = new Timer();
        timer.schedule(timerTask, 0, 1000);
       // SoundPoolUtils.getInstence.startSound(1);
       // SoundPoolUtils.getInstence(this).
        SoundPoolUtils.getInstence(this).startSound(SoundPoolUtils.COUNT_DOWN);
//        utils=new SoundPlayUtils(getApplicationContext());
//        mapSRC = new HashMap<Integer, Integer>();
//        mapSRC.put(1, utils.loadRes(R.raw.avchat_no_response));
//
//      //  mapSRC.put(3, utils.loadRes(R.raw.avchat_peer_busy));
////        soolId[2]=utils.loadRes(R.raw.avchat_no_response);
////        Log.e("-----",soolId[0]+""+soolId[1]+""+soolId[2]);
//utils.play(mapSRC.get(1),true);


    }

    @Override
    protected void onDestroy() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        if (timerTask != null) {
            timerTask = null;
        }
        minute = -1;
        second = -1;
        super.onDestroy();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }
}
